package com.springboot.uav.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by IntelliJ IDEA.
 * &#064;Author : Zys
 * &#064;create 2024/11/13 19:59
 */
public class SHA256Util {

    public static String sha256(String input)  {
        // 获取 MessageDigest 实例，指定算法为 SHA-256
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }

        // 计算输入数据的 SHA-256 摘要（哈希值）
        byte[] hashBytes = md.digest(input.getBytes());

        // 将哈希值转换为 16 进制字符串
        StringBuilder hexString = new StringBuilder();
        for (byte b : hashBytes) {
            // 将每个字节转为 2 位十六进制数
            hexString.append(String.format("%02x", b));
        }

        return hexString.toString();
    }
}
